package com.azure.controller;

import com.azure.service.LoginRegisterService;
import com.azure.service.Report.CategoryReportService;
import com.azure.service.Report.ProductReportService;
import com.azure.service.Report.TurnoverReportService;
import com.azure.service.Report.UserReportService;
import net.minidev.json.JSONArray;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * Created by liuhuaibin on 2018/3/13.
 *
 * 重定向和转发都可以跳转至其他路由（不可以直接跳转至页面）
 * redirect:/login    forward:/login  ("/"可以不写，但为了便于区分路由和页面加上“/”)
 *
 * 向模板页面发送数据可以使用Map和ModelAndView
 * 1.map.put(key,value);
 *   return "Login";
 * 2.modelAndView.addObject(name,value);
 *   modelAndView.setView();
 *   return modelAndView;
 */
@Controller
public class MainControlelr {

    @Autowired
    private LoginRegisterService loginRegisterService;
    @Autowired
    private UserReportService userReportService;
    @Autowired
    private CategoryReportService categoryReportService;
    @Autowired
    private ProductReportService productReportService;
    @Autowired
    private TurnoverReportService turnoverReportService;

    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
    DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyyMM");

    @RequestMapping("/")
    public String hello(){System.out.println("=====hello()===="); return "redirect:/login";}
    @RequestMapping("/login")
    public String login(){System.out.println("=====login()=====");return "Login";}
    @RequestMapping("/register")
    public String register(){return "Register";}
    @RequestMapping("/dologout")
    public String logout(HttpServletRequest request){
        System.out.println("----------dologout--------");
        request.getSession().setAttribute("username",null);
        return "redirect:/login";
    }
    @RequestMapping("/dologin")
    public String doLogin(String username, String password, HttpServletRequest request,Map kv){
        System.out.println("----------doLogin-----username: "+username+"----password: "+password+"--------------");

        boolean isSuccess = loginRegisterService.login(username, password);
        if(isSuccess){
            request.getSession().setAttribute("username",username);
            return "redirect:/userAnalysis";
        }else{
            kv.put("msg","用户名或密码错误!");
            return "Login";
        }
    }
    @RequestMapping("/doregister")
    public ModelAndView doregister(String username, String password,String password2, HttpServletRequest request){
        System.out.println("----------doRegister-----username: "+username+"----password: "+password+"--------------");
        if(username == null || "".equals(username)){
            ModelAndView modelAndView = new ModelAndView("Register");
            modelAndView.addObject("msg", "用户名为空！");
            return modelAndView;
        }else if(!password.equals(password2)){
            ModelAndView modelAndView = new ModelAndView("Register");
            modelAndView.addObject("msg", "两次密码输入不一致！");
            return modelAndView;
        }

        boolean isSuccess = loginRegisterService.register(username, password);
        if(isSuccess){
            request.getSession().setAttribute("username",username);
            ModelAndView modelAndView = new ModelAndView("UserAnalysis");
            return modelAndView;
        }else{
            ModelAndView modelAndView = new ModelAndView("register");
            modelAndView.addObject("msg", "用户名已占用！");
            return modelAndView;
        }
    }

    public static void main(String[] args){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyyMM");
        LocalDate searchMapDataDate = LocalDate.now().minusDays(1);
        LocalDate searchProvinceDataDate = LocalDate.of(new Date().getYear(),new Date().getMonth()+1-1,1);
        LocalDate searchDailyCountDate = LocalDate.now().minusMonths(1);
        System.out.println(dateTimeFormatter2.format(searchMapDataDate));
    }

    @RequestMapping("/userAnalysis")
    public ModelAndView userAnalysis2(){
//        LocalDate searchMapDataDate = LocalDate.now().minusDays(1);
//        LocalDate searchProvinceDataDate = LocalDate.of(1900+new Date().getYear(),new Date().getMonth()+1-1,1);
        LocalDate searchDailyCountDate = LocalDate.now().minusMonths(1);
        //测试用
        LocalDate searchMapDataDate = LocalDate.of(2018,4,15);
        LocalDate searchProvinceDataDate = LocalDate.of(2018,3,1);

        //javascript无法解析Map，需要使用JSONObject
        //List<Map<String,Object>> userMapData = userReportService.getUserMapData(dateTimeFormatter.format(searchMapDataDate));
        JSONArray userMapData = userReportService.getUserMapData(dateTimeFormatter.format(searchMapDataDate));
        Map<String,Object> userProvinceData = userReportService.getUserProvinceData(dateTimeFormatter2.format(searchProvinceDataDate));
        Map<String,Object> userDailyData = userReportService.getDailyCountData(dateTimeFormatter.format(searchDailyCountDate));
        System.out.println("=====userMapData: "+userMapData);
        System.out.println("=====userProvinceData: "+userProvinceData);
        System.out.println("=====userDailyData: "+userDailyData);
        ModelAndView modelAndView = new ModelAndView("UserAnalysis");
        modelAndView.addObject("mapdata", userMapData);
        modelAndView.addObject("provincename", userProvinceData.get("province"));
        modelAndView.addObject("provincevalue", userProvinceData.get("count"));
        modelAndView.addObject("monthdate", userProvinceData.get("monthdate"));
        modelAndView.addObject("dailydatestr", userDailyData.get("date"));
        modelAndView.addObject("dailydatecount", userDailyData.get("count"));
        modelAndView.addObject("daydate", userDailyData.get("daydate"));
        return modelAndView;
    }

    @RequestMapping("/categoryAnalysis")
    public ModelAndView categoryAnalysis(){
//        LocalDate searchDailyDataDate = LocalDate.now().minusDays(1);
//        LocalDate searchMonthlyDataDate = LocalDate.of(1900+new Date().getYear(),new Date().getMonth()+1-1,1);
        //测试用
        LocalDate searchDailyDataDate = LocalDate.of(2018,4,16);
        LocalDate searchMonthlyDataDate = LocalDate.of(2018,3,1);

        Map<String, Object> dailyCategoryData = categoryReportService.getDailyCategoryData(dateTimeFormatter.format(searchDailyDataDate));
        Map<String, Object> monthlyCategoryData = categoryReportService.getMonthlyCategoryData(dateTimeFormatter2.format(searchMonthlyDataDate));
        System.out.println("=====dailyCategoryData: "+dailyCategoryData);
        System.out.println("=====monthlyCategoryData: "+monthlyCategoryData);
        ModelAndView modelAndView = new ModelAndView("CategoryAnalysis");
        modelAndView.addObject("dailyCategoryNames", dailyCategoryData.get("categoryNames"));
        modelAndView.addObject("dailyCategoryHot", dailyCategoryData.get("categoryHot"));
        modelAndView.addObject("dailycategoryDate", dailyCategoryData.get("categoryDate"));
        modelAndView.addObject("monthlyCategoryNames", monthlyCategoryData.get("categoryNames"));
        modelAndView.addObject("monthlyCategoryHot", monthlyCategoryData.get("categoryHot"));
        modelAndView.addObject("monthlycategoryDate", monthlyCategoryData.get("categoryDate"));
        return modelAndView;
    }

    @RequestMapping("/productAnalysis")
    public ModelAndView productAnalysis(){
//        LocalDate searchDailyDataDate = LocalDate.now().minusDays(1);
//        LocalDate searchMonthlyDataDate = LocalDate.of(1900+new Date().getYear(),new Date().getMonth()+1-1,1);
        //测试用
        LocalDate searchDailyDataDate = LocalDate.of(2018,4,1);
        LocalDate searchMonthlyDataDate = LocalDate.of(2018,3,1);

        Map<String, Object> dailyProductData = productReportService.getDailyProductData(dateTimeFormatter.format(searchDailyDataDate));
        Map<String, Object> monthlyProductData = productReportService.getMonthlyProductData(dateTimeFormatter2.format(searchMonthlyDataDate));
        ModelAndView modelAndView = new ModelAndView("ProductAnalysis");
        modelAndView.addObject("dailyProductNames",dailyProductData.get("productNames"));
        modelAndView.addObject("dailyProductHot",dailyProductData.get("productHot"));
        modelAndView.addObject("dailyDate",dailyProductData.get("dailyDate"));
        modelAndView.addObject("monthlyProductNames",monthlyProductData.get("productNames"));
        modelAndView.addObject("monthlyProductHot",monthlyProductData.get("productHot"));
        modelAndView.addObject("monthlyDate",monthlyProductData.get("monthlyDate"));
        return modelAndView;
    }


    @RequestMapping("/turnoverAnalysis")
    public ModelAndView turnoverAnalysis(Map kv){
//        LocalDate searchDailyDataDate = LocalDate.now().minusDays(1);
//        LocalDate searchMonthlyDataDate = LocalDate.of(1900+new Date().getYear(),new Date().getMonth()+1-1,1);
        //测试用
        LocalDate searchDailyDataDate = LocalDate.of(2018,4,12);
        LocalDate searchMonthlyDataDate = LocalDate.of(2018,3,1);

        Map<String, Object> dailyTurnoverData = turnoverReportService.getDailyTurnoverData(dateTimeFormatter.format(searchDailyDataDate));
        Map<String, Object> monthlyTurnoverData = turnoverReportService.getMonthlyTurnoverData(dateTimeFormatter2.format(searchMonthlyDataDate));
        ModelAndView modelAndView = new ModelAndView("TurnoverAnalysis");
        modelAndView.addObject("dailyProvinceNames",dailyTurnoverData.get("dailyProvinceNames"));
        modelAndView.addObject("dailyTurnovers",dailyTurnoverData.get("dailyTurnovers"));
        modelAndView.addObject("dailyDate",dailyTurnoverData.get("dailyDate"));
        modelAndView.addObject("monthlyProvinceNames",monthlyTurnoverData.get("monthlyProvinceNames"));
        modelAndView.addObject("monthlyTurnovers",monthlyTurnoverData.get("monthlyTurnovers"));
        modelAndView.addObject("monthlyDate",monthlyTurnoverData.get("monthlyDate"));
        modelAndView.addObject("turnoverLineDate", dailyTurnoverData.get("turnoverLineDate"));
        modelAndView.addObject("turnoverLineData", dailyTurnoverData.get("turnoverLineData"));
        return modelAndView;
    }
}
